<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UserGroup
 *
 * @author Administrator
 */
class UserGroup extends CActiveRecord {

    public function rules() {
        return array(
            array('usergroup_name, usergroup_level,usergroup_description,usergroup_download_speed,usergroup_daily_play_num', 'required'),
            array('usergroup_level,usergroup_download_speed,usergroup_daily_play_num,usergroup_create_time', 'numerical'),
            array('usergroup_name,usergroup_level', 'unique', 'message' => '当前值已经被其他用户组占用')
        );
    }

    public function attributeLabels() {
        return array(
            'usergroup_id' => Yii::t('default', '编号'),
            'usergroup_name' => Yii::t('default', '用户组名称'),
            'usergroup_level' => Yii::t('default', '用户组等级'),
            'usergroup_description' => Yii::t('default', '用户组描述'),
            'usergroup_categorys' => Yii::t('default', '用户分类权限'),
            'usergroup_download_speed' => Yii::t('default', '下载速度'),
            'usergroup_add_title' => Yii::t('default', '添加新用户组'),
            'usergroup_create_user' => Yii::t('default', '创建人'),
            'usergroup_create_time' => Yii::t('default', '创建时间'),
            'usergroup_daily_play_num' => Yii::t('default', '每天点播上限'));
    }

    public function beforeSave() {
        if (parent::beforeSave()) {
            if ($this->isNewRecord) {
                $this->usergroup_create_time = time();
                $this->usergroup_create_user = Yii::app()->cookies->admin;
            }
        }
        return true;
    }

    /**
     * 删除之前验证是否有属于当前组的视频，用户资料，如果有不能删除 
     */
    public function beforeDelete() {
        if (parent::beforeDelete()) {
            if ($this->usergroup_level > 0) {
                $where = new CDbCriteria();
                $where->addCondition('video_play_level=:level', 'OR');
                $where->addCondition('video_down_level=:level', 'OR');
                $where->params[':level'] = $this->usergroup_level;
                $video = new Video();
                $count = $video->count($where);
                if ($count > 0) {
                    $this->addError('usergroup_level', Yii::t('default', '有视频权限属于当前用户u时不能删除'));
                    return false;
                }
                $where = new CDbCriteria();
                $where->addCondition('usergroup_level=:level');
                $where->params[':level'] = $this->usergroup_level;
                $video = new Video();
                $count = $video->count($where);
            } else {
                return false; //免费用户组不能删除
            }
        }
    }

    /**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{usergroup}}';
    }

    public function getParentOption() {
        $list = $this->findAll("", array("order" => "usergroup_level DESC"));
        $result = array();

        $result[0] = Yii::t('VODCMS_COM', '选择一个权限组');
        foreach ($list as $row) {
            $result[$row['usergroup_id']] = $row['usergroup_name'];
        }
        unset($list);
        return $result;
    }

    public function getSearch() {
        $criteria = new CDbCriteria();
        $criteria->order = 'usergroup_level DESC';
        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                    'pagination' => array(
                        'pageSize' => 5,
                    ),
                ));
    }

}

?>
